//
//  MKRefreshRightView.m
//  Category
//
//  Created by Burn on 2020/1/8.
//  Copyright © 2020 menksoft. All rights reserved.
//

#import "MKRefreshRightView.h"
#import "RefreshConst.h"
#import "UIView+Extension.h"

@interface MKRefreshRightView ()

@property(weak,nonatomic) id target;

@property(assign,nonatomic) SEL action;

@property(strong,nonatomic) UILabel *rightTitle;

@property(assign,nonatomic) BOOL panStateFooter;

@property(assign,nonatomic) CGFloat offsetx;

@end


@implementation MKRefreshRightView

+ (instancetype)rightWithRefreshingTarget:(id)target refreshingAction:(SEL)action{
    return [[[MKRefreshRightView alloc]init]rightWithRefreshingTarget:target refreshingAction:action];
}


- (instancetype)rightWithRefreshingTarget:(id)target refreshingAction:(SEL)action{
    MKRefreshRightView *rightView = [[MKRefreshRightView alloc]init];
    rightView.rightTitle = [[UILabel alloc]init];
    rightView.rightTitle.backgroundColor = [UIColor redColor];
    rightView.target = target;
    rightView.action = action;
    [rightView addSubview:self.rightTitle];
    rightView.rightTitle.text = @"footer";
    
    rightView.backgroundColor = [UIColor orangeColor];
    return rightView;
}
- (void)endRefreshing{
    [UIView animateWithDuration:.25 animations:^{
        self.scrollView.contentInset = UIEdgeInsetsMake(self.scrollView.contentInset.top, self.scrollView.contentInset.left, self.scrollView.contentInset.left, 0);
    }];
    self.state = RefreshIdle;
    self.panStateFooter = NO;
}

- (void)didMoveToSuperview{
    UICollectionView *collectionView = (UICollectionView *)self.scrollView;
    collectionView.alwaysBounceHorizontal = YES;
}

-(void)layoutSubviews{
    [super layoutSubviews];
    
    self.frame = CGRectMake(self.scrollView.contentSize.width > self.scrollView.width ? self.scrollView.contentSize.width : self.scrollView.width,0, MKRefreshContentInsetSpacing, self.scrollView.height - 10);
    self.rightTitle.frame = self.bounds;
    
}

- (void)scrollViewContentOffsetDidChange:(NSDictionary *)change{
    [super scrollViewContentOffsetDidChange:change];
    
    if (self.state == RefreshRefreshing) {return;}
    
    if (self.panStateFooter && self.state == RefreshIdle) {
        
        if (self.scrollView.height < self.scrollView.contentSize.height) {
            if (self.scrollView.contentSize.height - (self.scrollView.height + self.offsetx) <= -MKRefreshContentInsetSpacing) {
                self.state = RefreshRefreshing;
                [UIView animateWithDuration:.25 animations:^{
                    self.scrollView.contentInset = UIEdgeInsetsMake(self.scrollView.contentInset.top, self.scrollView.contentInset.left, self.scrollView.contentInset.bottom, self.scrollView.contentSize.width > self.scrollView.width ? MKRefreshContentInsetSpacing : self.scrollView.width - self.scrollView.contentSize.width + MKRefreshContentInsetSpacing);
                }];
                [self.target performSelector:self.action];
            }
        }else{
            if (self.offsetx >= MKRefreshContentInsetSpacing) {
                self.state = RefreshRefreshing;
                [UIView animateWithDuration:.25 animations:^{
                    self.scrollView.contentInset = UIEdgeInsetsMake(self.scrollView.contentInset.top, self.scrollView.contentInset.left, self.scrollView.contentInset.bottom, self.scrollView.contentSize.width > self.scrollView.width ? MKRefreshContentInsetSpacing : self.scrollView.width - self.scrollView.contentSize.width + MKRefreshContentInsetSpacing);
                }];
                [self.target performSelector:self.action];
            }
        }
    }
    
}

- (void)scrollViewContentSizeDidChange:(NSDictionary *)change{
    [super scrollViewContentSizeDidChange:change];
    self.y = self.scrollView.contentSize.height > self.scrollView.height ? self.scrollView.contentSize.height : self.scrollView.height;
}

- (void)scrollViewPanStateDidChange:(NSDictionary *)change{
    
    [super scrollViewPanStateDidChange:change];
    //
    //
    //    NSLog(@"footer - scrollViewPanStateDidChange");
    self.panStateFooter = [change[@"new"] integerValue] == 3;
    self.offsetx = self.scrollView.contentOffset.x;
    
    
}

@end
